package com.youbug.mall.coupon_sms.service.impl;

import com.youbug.mall.coupon_sms.entity.SmsMemberPrice;
import com.youbug.mall.coupon_sms.mapper.SmsMemberPriceMapper;
import com.youbug.mall.coupon_sms.service.ISmsMemberPriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * <p>
 * 商品会员价格 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-04-24
 */
@Service
public class SmsMemberPriceServiceImpl extends ServiceImpl<SmsMemberPriceMapper, SmsMemberPrice> implements ISmsMemberPriceService {

        @Override
        public IPage<SmsMemberPrice> getSmsMemberPricePage(QueryVo<SmsMemberPrice> queryVo) {

        Page<SmsMemberPrice> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<SmsMemberPrice> wrapper = new QueryWrapper<>();

        SmsMemberPrice smsMemberPrice = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(smsMemberPrice)){
            if(StringUtils.hasText(smsMemberPrice.getMemberLevelName())){
                wrapper.eq("member_level_name", smsMemberPrice.getMemberLevelName());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<SmsMemberPrice> page = this.page(paramPage, wrapper);

        return page;
        }

        @Transactional(transactionManager = "transactionManagerSms", rollbackFor = Exception.class)
        public boolean saveBatch(Collection<SmsMemberPrice> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
        }
}
